package com.wc.blue_bridge_test.base.T51;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * @Author congege
 * @Description 十六进制转八进制
 * https://lx.lanqiao.cn/problem.page?gpid=T51
 * @Date Created in 2023/12/23-23:26
 */
public class Main {
    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {

        char[] nums = new char[16];
        for (int i = 0; i < 10; i++) {
            nums[i] = (char) ('0' + i);
        }
        for (int i = 0; i < 6; i++) {
            nums[i + 10] = (char) ('A' + i);
        }
//        System.out.println(Arrays.toString(nums));
        HashMap<Character, String> map16 = new HashMap<>();
        for (int i = 0; i < 16; i++) {
            StringBuffer ss = new StringBuffer(Integer.toBinaryString(i));
            while (ss.length() < 4) {
                ss.insert(0, '0');
            }
            map16.put(nums[i], ss.toString());
        }
        HashMap<String, Character> map8 = new HashMap<>();
        for (int i = 0; i < 8; i++) {
            StringBuffer ss = new StringBuffer(Integer.toBinaryString(i));
            while (ss.length() < 3) {
                ss.insert(0, '0');
            }
            map8.put(ss.toString(), nums[i]);
        }

        int n = in.nextInt();
        while (n-- > 0) {
            String num16 = in.next();
            // 将16进制转换为2进制
            StringBuffer num2 = new StringBuffer();
            for (char c : num16.toCharArray()) {
                num2.append(map16.get(c));
            }
            while (num2.length() % 3 != 0) {
                num2.insert(0, '0');
            }
            // 将2进制转换为8进制
            StringBuffer num8 = new StringBuffer();
            int len = num2.length();
            for (int i = 0; i < len; i += 3) {
                String numSub = num2.substring(i, i + 3);
                num8.append(map8.get(numSub));
            }
            int idx = 0;
            // 将8进制前面的0去掉
            while (num8.charAt(idx) == '0') {
                idx++;
            }
            out.println(num8.substring(idx));
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}
